package com.woniuxy.controller;

import com.woniuxy.pojo.SaleChance;
import com.woniuxy.pojo.SalePlan;
import com.woniuxy.service.MyLog;
import com.woniuxy.service.SaleChanceService;
import com.woniuxy.util.DataGridView;
import com.woniuxy.util.ResultDto;
import com.woniuxy.vo.SaleChanceVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
public class SaleChanceController {
    private Logger logger= LoggerFactory.getLogger(this.getClass());
    @Autowired
    private SaleChanceService saleChanceService;

    @GetMapping(value = "saleChance")
    public DataGridView selectAllSaleChance(SaleChanceVo saleChanceVo){
        return saleChanceService.findAllSaleChance(saleChanceVo);
    }
    @MyLog(value = "添加销售机会")  //这里添加了AOP的自定义注解
    @PostMapping(value = "saleChance.do")
    public ResultDto addSaleChance(SaleChance saleChancee){
        boolean flag=false;
        //这里判断，在最开始创建销售机会的时候，是否有指派人这一操作，如果有指派人，那么需要设置该销售机会的状态，已指派则状态为1，“指派开发中”，否则为0，未指派
//        if (!saleChancee.getChanDueTo().equals("-200")){
//            saleChancee.setChanStatus(2);//该销售机会状态
//            saleChancee.setChanFlag(1);//未删除
//            flag=saleChanceService.addSaleChance(saleChancee);
//        }else {//这里如果没有指派人，那么就没有指派时间，状态是0，未指派。
        saleChancee.setChanStatus(1);
        saleChancee.setChanFlag(1);//未删除
        flag=saleChanceService.addSaleChance(saleChancee);
//        }
        if (flag){
            return ResultDto.SALECHANCE_ADD_SUCCESS;
        }else {
            return ResultDto.SALECHANCE_ADD_FAILURE;
        }
    }
    @MyLog(value = "删除销售机会")  //这里添加了AOP的自定义注解
    @DeleteMapping(value = "saleChance/{chanNo}")
    public ResultDto removeSaleChance(@PathVariable("chanNo") int chanNo){
        boolean flag=false;
        flag=saleChanceService.removeSaleChance(chanNo);
        if (flag){
            return ResultDto.SALECHANCE_DELETE_SUCCESS;
        }else {
            return ResultDto.SALECHANCE_DELETE_FAILURE;
        }

    }

    /**
     * 我们这里就是一个获取指派时间，有指派人才会返回时间，没有指派人就没有指派时间
     * @return
     */
    @GetMapping(value = "chanDueDate.do")
    public String chanDueDate(String chanDueTo){
        String date="";
        if (!chanDueTo.equals("-200")){//这个200就是在指派人是空的时候，我们设置的就是200
            logger.debug("进来了！！！！！！！！！！！！！！");
            //获取当前时间，然后赋值给指派时间
            Date d=new Date();
            //时间格式
            SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date=timeFormat.format(d);
            return date;
        }else {
            logger.debug("空！！！！！！！！！！！！！！");
            return "";
        }
    }

    /**
     * 修改，这里主要是指派之后的状态
     * @param saleChance
     * @return
     */
    @PostMapping(value = "updateSaleChance.do")
    public ResultDto updateSaleChance(SaleChance saleChance){
//        logger.debug("!!!!!!!!#########");
//        logger.debug(saleChance.getChanNo()+"");
//        logger.debug(saleChance.getChanCustName());
//        logger.debug(saleChance.getChanSource());
//        logger.debug(saleChance.getChanCreateBy());
//        logger.debug(saleChance.getChanCreateDate());
//        logger.debug(saleChance.getChanDueTo());
//        logger.debug(saleChance.getChanDescription());
//        logger.debug(saleChance.getChanStatus()+"");
        boolean flag=false;
        //前段我们在chanTo字段中设置的value是-200，代表没有指派人，这里判断如果有指派人，那么状态chanStatus就是2，代表已指派的状态
//        if (!saleChance.getChanDueTo().equals("-200")){
//            saleChance.setChanStatus(2);//设置已指派状态
//        }else if(saleChance.getChanDueTo().equals("-200")){
//            saleChance.setChanDueTo("");
//        }
        flag=saleChanceService.updateSaleChance(saleChance);
        if(flag){
            return ResultDto.SALECHANCE_UPDATE_SUCCESS;
        }else {
            return ResultDto.SALECHANCE_UPDATE_FAILURE;
        }
    }

    /**
     * 指派
     * @param saleChance
     * @return
     */
    @MyLog(value = "指派销售机会")  //这里添加了AOP的自定义注解
    @PostMapping(value = "saleChanceAssign.do")
    public ResultDto saleChanceAssign(SaleChance saleChance){
//        logger.debug("!!!!!!!!#########");
//        logger.debug(saleChance.getChanNo()+"");
//        logger.debug(saleChance.getChanCustName());
//        logger.debug(saleChance.getChanSource());
//        logger.debug(saleChance.getChanCreateBy());
//        logger.debug(saleChance.getChanCreateDate());
//        logger.debug(saleChance.getChanDueTo());
//        logger.debug(saleChance.getChanDescription());
//        logger.debug(saleChance.getChanStatus()+"");
        boolean flag=false;
        //前端我们在chanTo字段中设置的value是-200，代表没有指派人，这里判断如果有指派人，那么状态chanStatus就是2，代表已指派的状态
        if (!saleChance.getChanDueTo().equals("-200")){
            saleChance.setChanStatus(2);//设置已指派状态
        }else if(saleChance.getChanDueTo().equals("-200")){
            saleChance.setChanDueTo("");
        }
        flag=saleChanceService.updateSaleChance(saleChance);
        if(flag){
            return ResultDto.SALECHANCE_ASSIGN_SUCCESS;
        }else {
            return ResultDto.SALECHANCE_ASSIGN_FAILURE;
        }
    }

    /**
     * 找到已指派的以及该客户经理下的销售机会
     * @param saleChanceVo
     * @return
     */
    @GetMapping(value = "findAllSaleChanceByChanStatusAndChanDueTo.do")
    public DataGridView findAllSaleChanceByChanStatusAndChanDueTo(SaleChanceVo saleChanceVo){
        logger.debug("!!!!!!!!!!!!!!"+saleChanceVo.getChanDueTo()+"进来了嘛？");
        return saleChanceService.findAllSaleChanceByChanStatusAndChanDueTo(saleChanceVo);
    }

    /**
     * 通过销售机会的chanNo找到开发计划
     * @param chanNo
     * @return
     */
    @GetMapping(value = "findDevPlanByChanNo.do")
    public ResultDto findDevPlanByChanNo(int chanNo){
        List<SalePlan> salePlans=saleChanceService.findDevPlanByChanNo(chanNo);
        for (SalePlan s:salePlans
             ) {
            logger.debug("!!!!!!!!!!!!!!!!!");
            logger.debug(s.getPlanDate());
            logger.debug(s.getPlanItem());
        }
        ResultDto dto=new ResultDto();
        dto.setData(salePlans);
        return dto;
    }

    @PostMapping(value = "saveDevPlan.do")
    public ResultDto saveDevPlan(SalePlan salePlan){
        //SalePlan salePlan=new SalePlan();
        //salePlan.se
        boolean flag=false;
        flag=saleChanceService.saveDevPlan(salePlan);
        if (flag){
            return ResultDto.DEVPLAN_SAVE_SUCCESS;
        }else {
            return ResultDto.DEVPLAN_SAVE_FAILURE;
        }
    }

    /**
     * 修改开发计划
     * @param salePlan
     * @return
     */
    @MyLog(value = "修改开发计划")  //这里添加了AOP的自定义注解
    @PostMapping(value = "updateDevPlan.do")
    public ResultDto updateDevPlan(SalePlan salePlan){
        boolean flag=false;
        flag=saleChanceService.updateDevPlan(salePlan);
        if (flag){
            return ResultDto.DEVPLAN_UPDATE_SUCCESS;
        }else {
            return  ResultDto.DEVPLAN_UPDATE_FAILURE;
        }
    }

    /**
     * 根据状态查询销售机会列表
     * @param chanStatus
     * @return
     */
    @GetMapping(value = "saleChance/selectchanStatus")
    public DataGridView selectSaleChanceByChanStatus(int chanStatus){
        return saleChanceService.selectSaleChanceByChanStatus(chanStatus);
    }

    /**
     * 根据状态查询销售机会个数
     * @param chanStatus
     * @return
     */
    @PostMapping(value = "saleChance/count")
    public SaleChance selectcountByChanStatu(int chanStatus){
        SaleChance saleChance = saleChanceService.selectcountByChanStatu(chanStatus);
        return saleChance;
    }

    /**
     * 未指派的  得到的是个数
     * @return
     */
    @RequestMapping(value = "selectNoAssignSale")
    public List<SaleChance> selectNoAssignSale(){
        logger.info("我是未分配");
        return saleChanceService.selectNoAssignSale();
    }

    /**
     * 得到未指派的销售机会
     * @param saleChanceVo
     * @return
     */
    @GetMapping(value = "noAssignSaleChance")
    public DataGridView noAssignSaleChance(SaleChanceVo saleChanceVo){
        return saleChanceService.noAssignSaleChance(saleChanceVo);
    }

    /**
     * 待开发成功的
     * @param saleChanceVo
     * @return
     */
    @GetMapping(value = "selectNoSuccessDevPlan")
    public DataGridView selectNoSuccessDevPlan(SaleChanceVo saleChanceVo){
        return saleChanceService.selectNoSuccessDevPlan(saleChanceVo);
    }


}
